﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TemplateReport.cs" company="None">
//   <Author>Adrian von Allmen, 2013</Author>
// </copyright>
// <summary>
//   The template report.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CrumbleLibrary
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Resources;

    /// <summary>
    /// The template report.
    /// </summary>
    public class TemplateReport : IReport
    {
        #region Fields
        
        #region Translation Fields
        /// <summary>
        /// Report Language
        /// </summary>
        private CultureInfo reportLanguage;

        /// <summary>
        /// The translation helper.
        /// </summary>
        private LocalizationHelper translationHelper;

        #endregion

        #endregion

        #region Properities

        /// <summary>
        /// Gets Assembly Build Version.
        /// </summary>
        public string GetVersion
        {
            get
            {
                return this.GetType().Assembly.GetName().Version.ToString();
            }
        }

        /// <summary>
        /// Gets the ReportType for further handling
        /// </summary>
        public virtual HelperLibrary.ReportType GetReportType
        {
            get
            {
                return HelperLibrary.ReportType.MSRS;
            }
        }

        /// <summary>
        /// Gets a value indicating whether to load the report on startup.
        /// </summary>
        public bool LoadOnStartup
        {
            get
            {
                var returnValue = this.GetType().GetFields().FirstOrDefault(item => item.Name == "LoadReportOnStartup");
                if (returnValue != null && returnValue.GetValue(null) is bool)
                {
                    return (bool)returnValue.GetValue(null);
                }

                return true;
            }
        }

        /// <summary>
        /// Gets or sets the report language.
        /// </summary>
        public CultureInfo ReportLanguage
        {
            get
            {
                return this.reportLanguage;
            }

            set
            {
                this.translationHelper = new LocalizationHelper(value);
                this.reportLanguage = value;
            }
        }

        /// <summary>
        /// Gets or sets the parameter items.
        /// </summary>
        public virtual Dictionary<string, FilterParameterItem> ParameterItems { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Load the Report Definition/Layout based on Report Type;
        /// </summary>
        /// <returns> Report Definition as expected Object </returns>
        public object LoadReport()
        {
            switch (this.GetReportType)
            {
                case HelperLibrary.ReportType.MSRS:
                    {
                        var runtimeAssembly = this.GetType().Assembly;
                        var namespaces = runtimeAssembly.GetTypes().Select(t => t.Namespace).Distinct().FirstOrDefault();
                        Stream reportStream = runtimeAssembly.GetManifestResourceStream(namespaces + ".Report.rdlc");

                        return reportStream;
                    }

                default:
                    return null;
            }
        }

        /// <summary>
        /// Retrieve the instruction for retrieve/generate the data of the report
        /// </summary>
        /// <returns>The instructions as object </returns>
        public virtual object GetDataInstruction()
        {
            return null;
        }

        /// <summary>
        /// The get internal translation resource file
        /// </summary>
        /// <returns>The ResourceManager Reference of the assembly translation values </returns>
        public virtual ResourceManager GetInternalTranslaion()
        {
            var reportFile = new ResourceManager("ReportTranslation", this.GetType().Assembly);
            return reportFile;
        }

        /// <summary>
        /// Define the used Filter and their parameter
        /// </summary>
        /// <returns>A list with selected filters </returns>
        public virtual Dictionary<string, List<FilterParameterItem>> SetFilter()
        {
            var filterSetting = new Dictionary<string, List<FilterParameterItem>>();

            return filterSetting;
        }

        /// <summary>
        /// Convert the Values retrieved from Filter to apply in current report scope
        /// </summary>
        /// <param name="originParameterItems"> The origin parameter items. </param>
        /// <returns>The reformatted parameter items </returns>
        public virtual Dictionary<string, FilterParameterItem> ConvertValues(Dictionary<string, FilterParameterItem> originParameterItems)
        {
            return originParameterItems;
        }
        #endregion
    }
}
